package org.pixelami.twisted.internet.protocol
{
	public class Factory
	{
		private var _protocol:Object = null;
	    
	    public function set protocol(p:Object):void
	    {
	    	_protocol = p;
	    }
	    
	    public function get protocol():*
	    {
	    	return _protocol;
	    }

	    public var numPorts:int = 0;
	    public var noisy:Boolean = true;
		
		public function Factory()
		{
		}
		
		
	
	    public function doStart():void
	    {
	        /*
	        Make sure startFactory is called.
	
	        Users should not call this function themselves!
	        */
	        if(! this.numPorts)
	        {
	            if(this.noisy)
	            {
	                //log.msg("Starting factory %r" % this)
	                //trace("Starting factory - " + this)
	            }
	            this.startFactory()
	        }
	        this.numPorts = this.numPorts + 1
	    }
	
	    public function doStop():void
	    {
	    
	    
	        /*
	        Make sure stopFactory is called.
	
	        Users should not call this function themselves!
	        */
	        if(this.numPorts == 0)
	        {
	        
	        
	            // this shouldn't happen, but does sometimes and this is better
	            // than blowing up in assert as we did previously.
	            return
	        }
	        this.numPorts = this.numPorts - 1;
	        
	        if (!this.numPorts)
	        {
	            if (this.noisy)
	            {
	                //log.msg("Stopping factory %r" % this);
	                //trace("Stopping factory - " + this);
	            }
	            this.stopFactory();
	        }
	    }
	
	    public function startFactory():void
	    {
	        /*
	        This will be called before I begin listening on a Port or Connector.
	
	        It will only be called once, even if the factory is connected
	        to multiple ports.
	
	        This can be used to perform 'unserialization' tasks that
	        are best put off until things are actually running, such
	        as connecting to a database, opening files, etcetera.
	        */
	    }
	
	    public function stopFactory():void
	    {
	    
	    
	        /*
	        This will be called before I stop listening on all Ports/Connectors.
	
	        This can be overridden to perform 'shutdown' tasks such as disconnecting
	        database connections, closing files, etc.
	
	        It will be called, for example, before an application shuts down,
	        if it was connected to a port. User code should not call this function
	        directly.
	        */
	    }
	
	    public function buildProtocol(addr:Object):Object
	    {
	        /*
	        Create an instance of a subclass of Protocol.
	
	        The returned instance will handle input on an incoming server
	        connection, and an attribute \"factory\" pointing to the creating
	        factory.
	
	        Override this method to alter how Protocol instances get created.
	
	        @param addr: an object implementing L{twisted.internet.interfaces.IAddress}
	        */
	        var p:* = new this.protocol();
	        p.factory = this;
	        return p;
	    }
	}
}